let app = getApp()
import { gameList, promotionList, scoreMcItem, getLevel } from "../../api/score"
Page({
	data: {
		safetyArea: app.globalData.safetyArea + 16,
		statusHeight: app.globalData.window.statusHeight || 1,
		navheight: app.globalData.window.navheight || 1,
		id: null,
		actDeatails: {},
		navData: [
			{
				name: '海选晋级',
				num: 0
			}, {
				name: '对阵图',
			}
		],
		xzNavIndex: 0,
		searchData: {
			codeIsTop: false,
			scoreIsTop: false
		},
		scoreData: [],
		time: "",
		scrollTop: 0,
		encounterData: [],
		level: "",
		levelData: []
	},
	async onLoad(options) {
		this.setData({ id: options.id })
		this.getScoreMcItem()
		this.getLevelData()
		await this.getScorePanelFinal()
	},
	onPageScroll: function(e) { //获取滑动距离  控制head背景色
		this.setData({ scrollTop: e.scrollTop })
	},
	getScoreMcItem() {
		scoreMcItem(this.data.id).then(res => {
			this.setData({
				actDeatails: {
					actName: res.data.result.activityTitle,
					name: res.data.result.scoreProjectName,
					ticketName: res.data?.result?.ticketNameList ? res.data?.result?.ticketNameList.join(',') : '' ,
				},
				time: new Date().getTime()
			})
		})
	},
	async getScorePanelFinal() {
		await promotionList(this.data.id, { sort: 1, isPromotion: 1, isGuest: 0 }).then(res => {
			res.data.result.map(v => {
				v.score = this.toThousands(Number(v.score))
			})
			let navData = this.data.navData;
			navData[0].num = res.data.result.length;
			this.setData({
				scoreData: res.data.result,
				navData,
			})
		})
	},
	toThousands (params) {
		return (params * 1000).toString().split('.')[0] / 1000
	},
	arrSort(e) {
		const { type, sort } = e.currentTarget.dataset;
		let searchData = JSON.parse(JSON.stringify(this.data.searchData))
		let scoreData = JSON.parse(JSON.stringify(this.data.scoreData))
		let newArr = scoreData.sort((a, b) => {
			return sort ? b[type] - a[type] : a[type] - b[type]
		})
		type == 'number' && (searchData.codeIsTop = !searchData.codeIsTop)
		type == 'score' && (searchData.scoreIsTop = !searchData.scoreIsTop)
		this.setData({
			scoreData: newArr,
			searchData
		})
	},
	navGetScore(e) {
		const { index } = e.currentTarget.dataset;
		if (index == this.data.xzNavIndex) return false;
		this.setData({ xzNavIndex: index })
		if (index === 0) this.getScorePanelFinal();
		if (index === 1) this.getVjPrepare();
	},
	getVjPrepare() {
		gameList(this.data.id, { type: this.data.level }).then(res => {
			let d_l = Math.ceil(this.data.level / 2);
			this.setData({ encounterData:  Array(d_l).fill({ id: [], name: [] }) });
			let encounterData = JSON.parse(JSON.stringify(this.data.encounterData));
			res.data.result.map(v => {
				if (v.index > 0) {
					encounterData[Math.ceil(v.index / 2) - 1].id[v.index % 2 == 0 ? 1 : 0] = v.id;
					encounterData[Math.ceil(v.index / 2) - 1].name[v.index % 2 == 0 ? 1 : 0] = v.name || `${v.number} ${v.name || '号'}`;
				}
			})
			this.setData({ encounterData })
		})
	},
	autoScroll() {
        var that = this
        let query = wx.createSelectorQuery()
        query.select('.display').boundingClientRect(res => {
            that.setData({
                scrollTop: res.height * 100
            })
        })
        query.exec(res => {})
    },
	showError(t, type = 'none') {
		wx.showToast({
			title: t,
			icon: type,
			duration: 2000
		})
	},
	pushTime() {
		wx.navigateTo({ url: `/pages/time/index?id=${this.data.id }`})
	},
	getLevelData() {
		getLevel(this.data.id).then(res => {
			this.setData({
				levelData: res.data.result,
				level: res.data.result[0] || 32,
			})
		})
	},
	chengLevel(e) {
		const { level } = e.currentTarget.dataset;
		this.setData({ level });
		this.getVjPrepare();
	}
})
